
Sys.setlocale(locale = "chinese")
library(ggplot2)
library(ggthemes)
library(wesanderson)
library(gridExtra)

#source("./shiny/base.R")

#d <- get_data(dbname="stock", dbcoll = "bills_two")
library(readxl)

Args <- commandArgs()

basepath <- "J:/proj/stock/data"
suffix <- ".xlsx"

get_data <- function(basepath, year = 2018, date, filename) {
  return(read_excel(paste(basepath, year, date, filename, sep = "/")))
}

year <- 2018
date <- 20180212
basename <- paste("base", date, suffix, sep = "")
basefundname <- paste("basefund", date, suffix, sep = "")
groupname <- paste("group", date, suffix, sep = "")
groupfundname <- paste("groupfund", date, suffix, sep = "")

basedata <- get_data(basepath, date = date, filename = basename)
basefund <- get_data(basepath, date = date, filename = basefundname)
group <- get_data(basepath, date = date, filename = groupname)
groupfund <- get_data(basepath, date = date, filename = groupfundname)

get_intel_base <- function(basedata) {
  #base_intel <- c(-6, 0, 6)
  x <- within(basedata, {
    y <- NA
    y[basedata$`涨幅%` <= -9.9] <- "-9.9-"
    y[basedata$`涨幅%` <= -6 & basedata$`涨幅%` > -9.9] <- "-6-9.9"
    y[basedata$`涨幅%` < 0 & basedata$`涨幅%` > -6] <- "-0-6"
    y[basedata$`涨幅%` > 0 & basedata$`涨幅%` < 6] <- "0+6"
    y[basedata$`涨幅%` >= 6 & basedata$`涨幅%` < 9.9] <- "6+9.9"
    y[basedata$`涨幅%` >= 9.9] <- "9.9+"
    y[which(basedata$`涨幅%` == 0)] <- "0"
  })
  base_intel <- as.data.frame(table(x$y))
  return(base_intel)
}

get_intel_group <- function(group) {
  #group_intel <- c(-10, -5, -3, -1, 0, 1, 3, 5, 10)
  x <- within(group, {
    y <- NA
  y[which(group$`涨幅%` >= 10)] <- "10+"
  y[group$`涨幅%` < 10 & group$`涨幅%` >= 5] <- "5+10"
  y[group$`涨幅%` < 5 & group$`涨幅%` >= 3] <- "3+5"
  y[group$`涨幅%` < 3 & group$`涨幅%` > 1] <- "1+3"
  y[group$`涨幅%` < 1 & group$`涨幅%` > 0] <- "0+1"
  y[group$`涨幅%` == 0] <- "0"
  y[group$`涨幅%` <= -10] <- "-10-"
  y[group$`涨幅%` > -10 & group$`涨幅%` <= -5] <- "-10-5"
  y[group$`涨幅%` > -5 & group$`涨幅%` <= -3] <- "-5-3"
  y[group$`涨幅%` > -3 & group$`涨幅%` <= -1] <- "-3-1"
  y[group$`涨幅%` > -1 & group$`涨幅%` < 0] <- "-1-0"
  })
  return(as.data.frame(table(x$y)))
}

get_intel_basefund <- function(basefund) {
  #basefund_intel <- c(-100, -50, -20, -10, -5, -2, -1, 0, 1, 2, 5, 10, 20, 50, 100)
  num_base <- 100000000
  x <- within(basefund, {
    y <- NA
    y[basefund$净流入 > 100*num_base] <- "100+"
    y[basefund$净流入 >= 50*num_base & basefund$净流入 < 100*num_base] <- "50+100"
    y[basefund$净流入 >= 20*num_base & basefund$净流入 < 50*num_base] <- "20+50"
    y[basefund$净流入 >= 10*num_base & basefund$净流入 < 20*num_base] <- "10+20"
    y[basefund$净流入 >= 5*num_base & basefund$净流入 < 10*num_base] <- "5+10"
    y[basefund$净流入 >= 2*num_base & basefund$净流入 < 5*num_base] <- "2+5"
    y[basefund$净流入 >= 1*num_base & basefund$净流入 < 2*num_base] <- "1+2"
    y[basefund$净流入 > 0 & basefund$净流入 < 1*num_base] <- "0+1"
    y[basefund$净流入 <= -100*num_base] <- "-100-"
    y[basefund$净流入 > -100*num_base & basefund$净流入 <= -50*num_base] <- "-50-100"
    y[basefund$净流入 > -50*num_base & basefund$净流入 <= -20*num_base] <- "-20-50"
    y[basefund$净流入 > -20*num_base & basefund$净流入 <= -10*num_base] <- "-10-20"
    y[basefund$净流入 > -10*num_base & basefund$净流入 <= -5*num_base] <- "-5-10"
    y[basefund$净流入 > -5*num_base & basefund$净流入 <= -2*num_base] <- "-2-5"
    y[basefund$净流入 > -2*num_base & basefund$净流入 <= -1*num_base] <- "-1-2"
    y[basefund$净流入 > -1*num_base & basefund$净流入 < 0] <- "-0-1"
    y[basefund$净流入 == 0] <- "0"
  })
  return(as.data.frame(table(x$y)))
}

get_intel_groupfund <- function(groupfund) {
  #groupfund_intel <- c(-200, -100, -50, -20, -10, -5, -2, -1, 0, 1, 2, 5, 10, 20, 50, 100, 200)
  num_base <- 100000000
  x <- within(groupfund, {
    y <- NA
    y[groupfund$净流入 > 200*num_base] <- "200+"
    y[groupfund$净流入 >= 100*num_base & groupfund$净流入 < 200*num_base] <- "100+200"
    y[groupfund$净流入 >= 50*num_base & groupfund$净流入 < 100*num_base] <- "50+100"
    y[groupfund$净流入 >= 10*num_base & groupfund$净流入 < 50*num_base] <- "10+50"
    y[groupfund$净流入 >= 5*num_base & groupfund$净流入 < 10*num_base] <- "5+10"
    y[groupfund$净流入 >= 1*num_base & groupfund$净流入 < 5*num_base] <- "1+5"
    y[groupfund$净流入 > 0 & groupfund$净流入 < 1*num_base] <- "0+1"
    y[groupfund$净流入 <= -200*num_base] <- "-200-"
    y[groupfund$净流入 > -200*num_base & groupfund$净流入 <= -100*num_base] <- "-100-200"
    y[groupfund$净流入 > -100*num_base & groupfund$净流入 <= -50*num_base] <- "-50-100"
    y[groupfund$净流入 > -50*num_base & groupfund$净流入 <= -10*num_base] <- "-10-50"
    y[groupfund$净流入 > -10*num_base & groupfund$净流入 <= -5*num_base] <- "-5-10"
    y[groupfund$净流入 > -5*num_base & groupfund$净流入 <= -1*num_base] <- "-1-5"
    y[groupfund$净流入 > -1*num_base & groupfund$净流入 < 0] <- "-0-1"
    y[groupfund$净流入 == 0] <- "0"
  })
  return(as.data.frame(table(x$y)))
}



get_intel_base_ext <- function(basedata) {
  x <- within(basedata, {
    level <- NA
    level[basedata$`涨幅%` <= -9.9] <- "-9.9-"
    level[basedata$`涨幅%` <= -6 & basedata$`涨幅%` > -9.9] <- "-6-9.9"
    level[basedata$`涨幅%` < 0 & basedata$`涨幅%` > -6] <- "-0-6"
    level[basedata$`涨幅%` > 0 & basedata$`涨幅%` < 6] <- "0+6"
    level[basedata$`涨幅%` >= 6 & basedata$`涨幅%` < 9.9] <- "6+9.9"
    level[basedata$`涨幅%` >= 9.9] <- "9.9+"
    level[which(basedata$`涨幅%` == 0)] <- "0"
  })
  return(x)
}

get_intel_group_ext <- function(group) {
  #group_intel <- c(-10, -5, -3, -1, 0, 1, 3, 5, 10)
  x <- within(group, {
    y <- NA
    y[which(group$`涨幅%` >= 10)] <- "10+"
    y[group$`涨幅%` < 10 & group$`涨幅%` >= 5] <- "5+10"
    y[group$`涨幅%` < 5 & group$`涨幅%` >= 3] <- "3+5"
    y[group$`涨幅%` < 3 & group$`涨幅%` > 1] <- "1+3"
    y[group$`涨幅%` < 1 & group$`涨幅%` > 0] <- "0+1"
    y[group$`涨幅%` == 0] <- "0"
    y[group$`涨幅%` <= -10] <- "-10-"
    y[group$`涨幅%` > -10 & group$`涨幅%` <= -5] <- "-10-5"
    y[group$`涨幅%` > -5 & group$`涨幅%` <= -3] <- "-5-3"
    y[group$`涨幅%` > -3 & group$`涨幅%` <= -1] <- "-3-1"
    y[group$`涨幅%` > -1 & group$`涨幅%` < 0] <- "-1-0"
  })
  return(x)
}

get_intel_basefund_ext <- function(basefund) {
  #basefund_intel <- c(-100, -50, -20, -10, -5, -2, -1, 0, 1, 2, 5, 10, 20, 50, 100)
  num_base <- 100000000
  x <- within(basefund, {
    y <- NA
    y[basefund$净流入 > 100*num_base] <- "100+"
    y[basefund$净流入 >= 50*num_base & basefund$净流入 < 100*num_base] <- "50+100"
    y[basefund$净流入 >= 20*num_base & basefund$净流入 < 50*num_base] <- "20+50"
    y[basefund$净流入 >= 10*num_base & basefund$净流入 < 20*num_base] <- "10+20"
    y[basefund$净流入 >= 5*num_base & basefund$净流入 < 10*num_base] <- "5+10"
    y[basefund$净流入 >= 2*num_base & basefund$净流入 < 5*num_base] <- "2+5"
    y[basefund$净流入 >= 1*num_base & basefund$净流入 < 2*num_base] <- "1+2"
    y[basefund$净流入 > 0 & basefund$净流入 < 1*num_base] <- "0+1"
    y[basefund$净流入 <= -100*num_base] <- "-100-"
    y[basefund$净流入 > -100*num_base & basefund$净流入 <= -50*num_base] <- "-50-100"
    y[basefund$净流入 > -50*num_base & basefund$净流入 <= -20*num_base] <- "-20-50"
    y[basefund$净流入 > -20*num_base & basefund$净流入 <= -10*num_base] <- "-10-20"
    y[basefund$净流入 > -10*num_base & basefund$净流入 <= -5*num_base] <- "-5-10"
    y[basefund$净流入 > -5*num_base & basefund$净流入 <= -2*num_base] <- "-2-5"
    y[basefund$净流入 > -2*num_base & basefund$净流入 <= -1*num_base] <- "-1-2"
    y[basefund$净流入 > -1*num_base & basefund$净流入 < 0] <- "-0-1"
    y[basefund$净流入 == 0] <- "0"
  })
  return(x)
}

get_intel_groupfund_ext <- function(groupfund) {
  #groupfund_intel <- c(-200, -100, -50, -20, -10, -5, -2, -1, 0, 1, 2, 5, 10, 20, 50, 100, 200)
  num_base <- 100000000
  x <- within(groupfund, {
    y <- NA
    y[groupfund$净流入 > 200*num_base] <- "200+"
    y[groupfund$净流入 >= 100*num_base & groupfund$净流入 < 200*num_base] <- "100+200"
    y[groupfund$净流入 >= 50*num_base & groupfund$净流入 < 100*num_base] <- "50+100"
    y[groupfund$净流入 >= 10*num_base & groupfund$净流入 < 50*num_base] <- "10+50"
    y[groupfund$净流入 >= 5*num_base & groupfund$净流入 < 10*num_base] <- "5+10"
    y[groupfund$净流入 >= 1*num_base & groupfund$净流入 < 5*num_base] <- "1+5"
    y[groupfund$净流入 > 0 & groupfund$净流入 < 1*num_base] <- "0+1"
    y[groupfund$净流入 <= -200*num_base] <- "-200-"
    y[groupfund$净流入 > -200*num_base & groupfund$净流入 <= -100*num_base] <- "-100-200"
    y[groupfund$净流入 > -100*num_base & groupfund$净流入 <= -50*num_base] <- "-50-100"
    y[groupfund$净流入 > -50*num_base & groupfund$净流入 <= -10*num_base] <- "-10-50"
    y[groupfund$净流入 > -10*num_base & groupfund$净流入 <= -5*num_base] <- "-5-10"
    y[groupfund$净流入 > -5*num_base & groupfund$净流入 <= -1*num_base] <- "-1-5"
    y[groupfund$净流入 > -1*num_base & groupfund$净流入 < 0] <- "-0-1"
    y[groupfund$净流入 == 0] <- "0"
  })
  return(x)
}

base_intel <- get_intel_base(basedata)
group_intel <- get_intel_group(group)
basefund_intel <- get_intel_basefund(basefund)
groupfund_intel <- get_intel_groupfund(groupfund)
base_order <- c("-9.9-", "-6-9.9", "-0-6", "0", "0+6", "6+9.9", "9.9+")
basefund_order <- c("-100-", "-50-100","-20-50", "-10-20", "-5-10", "-2-5", "-1-2", "-0-1", "0", 
                    "0+1", "1+2", "2+5", "5+10", "10+20", "20+50", "50+100", "100+")
group_order <- c("-10-", "-10-5", "-5-3", "-3-1", "-1-0", "0",
                 "0+1", "1+3", "3+5", "5+10", "10+")
groupfund_order <- c("-200-", "-100-200", "-50-100", "-10-50", "-5-10", "-1-5",
                     "-0-1", "0", "0+1", "1+5", "5+10", "10+50", "50+100", 
                     "100+200", "200+")

base_order_func <- function(x) { factor(x, levels=base_order)}
basefund_order_func <- function(x) {factor(x, levels=basefund_order)}
group_order_func <- function(x) {factor(x, levels=group_order)}
groupfund_order_func <- function(x) { factor(x, levels=groupfund_order) }

draw_base_intel <- function(data, title="None", xfunc) {
  ccount <- length(data$Var1)
  cleft <- nrow(data[substring(data$Var1,0,1) == "-",])
  if (cleft >= ccount) cleft = cleft -1
  cright <- ccount - cleft -1 
  cat("today top count ", ccount, "cleft : ", cleft, ", cright : ", cright, "\n")
  
  if (cright < 0) cright = 0
  ccolor <- c(rep("blue", cleft), rep("white", 1), rep("red", cright))
  
  p = ggplot(data, aes(x=xfunc(Var1), y=Freq), group = factor(1)) + 
    geom_bar(stat = "identity", width = 0.99, fill=ccolor) + theme_economist() + 
    geom_text(aes(label=Freq, vjust=-0.8, hjust=0.5, color=Var1), 
              show.legend = FALSE) +
    labs(title=title, x="Level", y="Count")
  return(p)
}

draw_all <- function() {
  #par(mfrow=c(2,2), mar=c(.8,.8,.8,.8))
  
  a <- draw_base_intel(base_intel, "涨停", base_order_func)
  b <- draw_base_intel(group_intel, "板块涨幅", group_order_func)
  c <- draw_base_intel(basefund_intel, "个股资金", basefund_order_func)
  d <- draw_base_intel(groupfund_intel, "板块资金", groupfund_order_func)
  grid.arrange(a, b, c, d, ncol = 2, nrow = 2)
}

draw_all_ext <- function() {
  xx <- get_intel_base_ext(basedata)
  xy <- get_intel_group_ext(group)
  xz <- get_intel_basefund_ext(basefund)
  xd <- get_intel_groupfund_ext(groupfund)
  par(mfrow=c(2,2), mar=c(.8,.8,.8,.8))
  a <- ggplot(xx, aes(x=`涨幅%`)) + 
    geom_histogram(binwidth=1.0, position="dodge")
  b <- ggplot(xy, aes(x=`涨幅%`, fill=y)) + 
    geom_histogram(binwidth=1.0, position="dodge")
  c <- ggplot(xz, aes(x=净流入, fill=y)) + 
    geom_histogram(binwidth=1.0, position="dodge")
  d <- ggplot(xd, aes(x=净流入, fill=y)) + 
    geom_histogram(binwidth=1.0, position="dodge")
  grid.arrange(a,b,c,d, ncol = 2, nrow = 2)
}

width_1 <- 1920
height_1 <- 1080
export_png <- TRUE

if (export_png) {
  png(filename = paste("j:/proj/stock_", date, ".png", sep = ""), width = width_1, height = height_1)
  draw_all()
} else {
  draw_all()
}
if (export_png) dev.off()

